const http = require("http");
const mysql = require("mysql")
const serve = http.createServer((req,res)=>{
    // res.writeHead(200,{'Context-Type':'text/html'});
    // res.write('<head><meta charset="utf-8"/></head>'); 
    // 添加响应头
    res.setHeader("Access-Control-Allow-Origin", "*"); 
    const connect = mysql.createConnection({
        
            host     : 'localhost',
            user     : 'root',
            password : '123456',
            database : 'user'
          
    })
    connect.connect()
    //查询
    let sql = "select * from user";
    connect.query(sql,(err,result)=>{
        if(err){
            throw err;
        }
        let data = JSON.stringify(result);
        res.end(data)
    })

})

serve.listen(8080)
console.log("服务器已经启动");